package com.videoadmin.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.videoadmin.dao.IPointsCardDAO;
import com.videoadmin.factory.DAOFactory;
import com.videoadmin.pojo.PointsCard;
import com.videoadmin.utils.GetPoints;
import com.videoadmin.utils.Md5;

public class AddPointServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public AddPointServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 获取参数
		// 数量
		int number = Integer.parseInt(request.getParameter("cardNumber"));
		// 类型 0表示点数 1表示包月
		int type = Integer.parseInt(request.getParameter("cardType"));
		// 相关点数
		int points = Integer.parseInt(request.getParameter("cardPoints"));

		// 增加充值卡
		IPointsCardDAO pointsCardDAO = DAOFactory.getIPointsCardDAOInstance();
		
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < number; i++) {
			PointsCard card = new PointsCard();

			// 设置有效期
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.MONTH, 12);
			card.setDeadline(calendar.getTime());
			card.setPoints(GetPoints.getPointsFromType(type, points));
			card.setPassword((type == 0 ? "P" : "B") + 
					Md5.getMd5String(new Date().toString() + i).substring(0, 15));
			pointsCardDAO.saveOrUpdate(card);
			list.add(card.getPassword());
			
		}
		
		request.setAttribute("cards", list);
		request.setAttribute("success", "添加成功");
		request.getRequestDispatcher("add_pointcard.jsp").forward(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
